package com.lijin.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DataUtil {

  /**
   * 时间戳转换成日期格式字符串
   *
   * @param seconds 精确到秒的字符串
   */
  public static String timeStamp2Date(String seconds, String format) {
    if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
      return "";
    }
    if (format == null || format.isEmpty()) {
      format = "yyyy-MM-dd HH:mm:ss";
    }
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return sdf.format(new Date(Long.valueOf(seconds + "000")));
  }

  /**
   * 日期格式字符串转换成时间戳
   *
   * @param date 字符串日期
   * @param format 如：yyyy-MM-dd HH:mm:ss
   */
  public static String date2TimeStamp(String date_str, String format) {
    try {
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      return String.valueOf(sdf.parse(date_str).getTime() / 1000);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "";
  }

  /**
   * 取得当前时间戳（精确到秒）
   */
  public static String timeStamp() {
    long time = System.currentTimeMillis();
    String t = String.valueOf(time / 1000);
    return t;
  }

  public static String stringToString(String time) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = sdf.parse(time);
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    return sdf2.format(date);
  }

  public static String dateToString() throws ParseException {
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String time = sdf2.format(new Date());
    return time;
  }

  public static String chuliCode(String code) {
    String str[] = code.split(",");
    String finalStr = "";
    for (int i = 0; i < str.length; i++) {
      if (Integer.parseInt(str[i]) < 10) {
        str[i] = "0" + Integer.parseInt(str[i]);
      }
      finalStr += str[i] + ",";
    }
    return finalStr.substring(0, finalStr.length() - 1);
  }

  public static String DataToString(String str) throws ParseException {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Long time = new Long(str);
    String date = timeStamp2Date(str, "yyyy-MM-dd HH:mm:ss");
    String d1 = format.format(format.parse(date).getTime());
    String d2 = format.format(format.parse(date).getTime() - 600000 - 600000);
    return d2;
  }

  public static String Data1ToString(Date str) throws ParseException {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String time = format.format(str);

    return time;
  }

  public static String Data1ToShortString(Date str) throws ParseException {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    String time = format.format(str);

    return time;
  }

  public static String Data1ToMoreShortString(Date str) throws ParseException {
    SimpleDateFormat format = new SimpleDateFormat("yyyy");
    String time = format.format(str);

    return time;
  }

  public static String text(String[] str) {
    StringBuffer sb = new StringBuffer();
    if (str != null) {
      for (String obStr : str) {
        sb.append(Integer.parseInt(obStr) + ",");
        //sb.append(obStr + ",");
      }
    }

    int len = sb.length();
    if (len > 0) {
      sb.deleteCharAt(len - 1);
    }
    return sb.toString();
  }


  public static String chuliCodeRemove0(String code) {
    String str[] = code.split(",");
    String finalStr = "";
    for (int i = 0; i < str.length; i++) {
      finalStr += Integer.parseInt(str[i]) + ",";
    }
    return finalStr.substring(0, finalStr.length() - 1);
  }

}
